﻿var AJAXCODE = new Object();
AJAXCODE.Login = 101001;
AJAXCODE.GetUser = 111001;
AJAXCODE.Quit = 111002;
AJAXCODE.ResetPWD = 111003;
AJAXCODE.SaveClass = 111101;
AJAXCODE.DeleteClass = 111102;
AJAXCODE.CheckImage = 111201;
AJAXCODE.DeleteArticle = 111202;
AJAXCODE.SaveArticle = 111203;
AJAXCODE.SaveImage = 111204;
AJAXCODE.DeleteImage = 111205;
AJAXCODE.DeleteFile = 111301;
AJAXCODE.GetFileList = 111302;
AJAXCODE.FiUploader = 111303;
AJAXCODE.NewFolder = 111304;
AJAXCODE.CheckFolder = 111305;
AJAXCODE.FiUploaderM = 111306;
AJAXURL = "x/ajax.ashx";


/*
* 获得页码逻辑
* s : 页大小
* i : 当前页
* rc ：总行数
* limt ：页码限制
* q : 查询字符串
*/
function getPager(size, idx, rc, limt, q) {
    var i;  //循环变量
    var cmax;

    //获得总页数
    var pc = (rc % size > 0) ? parseInt(rc / size) + 1 : parseInt(rc / size);
    if (pc < 1) {
        return "";
    }
    var pager = idx == 1 ? '<a class="prev dis">上一页</a>' : '<a class="prev" href="?i=' + (idx - 1).toString() + ((q == null || q == '') ? '' : '&' + q) + '">上一页</a>';

    if (idx - limt < 1) {
        i = 1;
    }
    else {
        i = idx - limt;
        pager += '<a class="dis">...</a>';
    }

    cmax = idx + limt > pc ? pc : idx + limt;

    while (i <= cmax) {
        pager += i == idx ? '<a class="cur">' + i.toString() + '</a>' : '<a href="?i=' + i.toString() + ((q == null || q == '') ? '' : '&' + q) + '">' + i.toString() + '</a>';
        i++;
    }

    pager += idx + limt < pc ? '<a class="dis">...</a>' : '';

    pager += idx == pc ? '<a class="next dis">下一页</a> ' : '<a class="next" href="?i=' + (idx + 1).toString() + ((q == null || q == '') ? '' : '&' + q) + '">下一页</a>';
    return pager;
}

/*
* 功能:弹出消息框
* flag : 标识
* title ： 标题
* message ：消息
* url ：跳转地址
*/
function showmsg(flag, title, message, url) {
    alert(message);
}

/*
*Ajax
*/
function fx(p, u, b) {
    var url;
    var back;
    var obj;
    //如果一个参数都没有
    if (p == null) {
        url = AJAXURL;
        obj = { r: Math.random() };
        back = null;
    }
    //如果u==null,说明该函数只有一个参数
   
    else if (u == null) {
        //只处理p为参数集的情况,其他情况暂不处理
        if ((typeof (p)).toString().toLocaleLowerCase() == "object") {
            url = AJAXURL;
            obj = p;
            back = null;
        }
        else {
            alert("参数错误！");
            return;
        }
    }
    //如果b==null,说明该函数只有2个参数
    else if (b == null) {
        //p为参数集，u为ajax地址的情况
        if ((typeof (p)).toString().toLocaleLowerCase() == "object" && (typeof (u)).toString().toLowerCase() == "string") {
            url = u == "" ? AJAXURL : u;
            obj = p;
            back = null;
        }
        //p为参数集，u为回调函数的情况
        else if ((typeof (p)).toString().toLocaleLowerCase() == "object" && (typeof (u)).toString().toLowerCase() == "function") {
            url = AJAXURL;
            obj = p;
            back = u;
        } //其他情况暂不处理
        else {
            alert("参数错误！");
            return;
        }
    }
    //如果有三个参数
    else {
        //只处理p为参数集，u为ajax地址，b为回调函数的情况，其他暂不处理
        if ((typeof (p)).toString().toLocaleLowerCase() == "object" && (typeof (u)).toString().toLowerCase() == "string" && (typeof (b)).toString().toLocaleLowerCase() == "function") {
            url = u == "" ? AJAXURL : u;
            obj = p;
            back = b;
        }
        else {
            alert("参数错误！");
            return;
        }
    }
    $.ajax({
        url: url,
        data: obj,
        type: "POST",
        dataType: "json",
        success: function (data) {
            if (data == null) {
                return;
            }
            if (data.Success) {
                if (data.Message != null && data.Message != "") {
                    alert(data.Message);
                }
                if (data.Url != null && data.Url != "") {
                    document.location = data.Url;
                }
            }
            else {
                alert(data.Message);
                if (data.Code == -1) {
                    document.location = "login.aspx";
                }
            }
            if (back != null) {
                back(data);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("系统异常！详细信息：" + XMLHttpRequest.responseText);
        }
    });
}

function setsv(e, v) {
    if (e == null) {
        return;
    }
    var selector;
    if ((typeof (e)).toString().toLowerCase() == "string" && e != "") {
        selector = $(e);
    }
    else {
        selector = e;
    }
    if (v == "") {
        selector.children("option:eq(0)").attr("selected", "selected");
    }
    else {
        selector.children("option").each(function () {
            if ($(this).attr("value") == v) {
                $(this).attr("selected", "selected");
            }
        });
    }
}

function getsv(e) {
    if (e == null) {
        return;
    }
    var selector;
    if ((typeof (e)).toString().toLowerCase() == "string" && e != "") {
        selector = $(e);
    }
    else {
        selector = e;
    }

    var v = -1;
    if (!selector) {
        return;
    }
    selector.children("option").each(function () {
        if ($(this).attr("selected") && $(this).attr("selected").toString().toLowerCase() == "selected") {
            v = $(this).attr("value");
        }
    });
    if (v == -1) {
        v = selector.children("option:eq(0)").attr("value");
    }
    return v;
}